<style>
	textarea {
		width: 800px;
		height: 600px;
		display: block;
	}
</style>
<?php
	include('config.php');
	
	

	//Wordpress Config
	$WP_PATH = '../wp-config.php';
	$WP_REPLACES = [
	 ["/define\\('DB_NAME', '[^']*'\\);/", "define('DB_NAME', '" . $DATABASE . "');"],
	 ["/define\\('DB_USER', '[^']*'\\);/", "define('DB_USER', '" . $USERNAME . "');"],
	 ["/define\\('DB_PASSWORD', '[^']*'\\);/", "define('DB_PASSWORD', '" . $PASSWORD . "');"],
	 ["/define\\('DB_HOST', '[^']*'\\);/", "define('DB_HOST', '" . $HOST . "');"]
	];
	file_replace($WP_PATH, $WP_REPLACES);
	

	
	//phpBB Config
	$PB_PATH = '../forum/config.php';	
	$PB_REPLACES = [
		["/\\\$dbhost = '[^']*';/" , "\$dbhost = '" . $HOST . "';"],
		["/\\\$dbname = '[^']*';/" , "\$dbname = '" . $DATABASE . "';"],
		["/\\\$dbuser = '[^']*';/" , "\$dbuser = '" . $USERNAME . "';"],
		["/\\\$dbpasswd = '[^']*';/" , "\$dbpasswd = '" . $PASSWORD . "';"]
	];
	file_replace($PB_PATH, $PB_REPLACES);

	
	//MySQLDumper Config
	$MSD_PATH = 'msd/work/config/mysqldumper.php';
	$MSD_REPLACES = [
		["/\\\$config\\['dbhost'\\] = '[^']*';/", "\$config['dbhost'] = '" . $HOST . "';"],
		["/\\\$config\\['dbuser'\\] = '[^']*';/", "\$config['dbuser'] = '" . $USERNAME . "';"],
		["/\\\$config\\['dbpass'\\] = '[^']*';/", "\$config['dbpass'] = '" . $PASSWORD . "';"]
	];
	file_replace($MSD_PATH, $MSD_REPLACES);
	
	/*
	 * Replace a set of objects in a file
	 * arg: 
	 *  + $path: file path
	 *  + $replaces: 2D Array, each object is an 2-element array [regex, replace_string]
	 *  for example: 
	      
		$replaces = [
			['/a\d+b/g', 'a-b'],
			['/a\d+b/g', 'a-b']
		  ];
	 */

	echo 'setup success!';
	function file_replace($path, $replaces) {
		$str = file_get_contents($path);
		for($i = 0, $L = count($replaces); $i < $L; $i++) {
			$str = preg_replace($replaces[$i][0], $replaces[$i][1], $str);
		}
		file_put_contents($path, $str);
	}

echo "ok";
?>


<?php
/*
:: Wordpress Config
path: /wp-config.php
define('DB_NAME', 'doanhoi');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');


:: phpBB Config
path: /forum/config.php
$dbhost = 'localhost';
$dbname = 'doanhoi';
$dbuser = 'root';
$dbpasswd = '';


:: MySQLDumper Config
path: /setup/msd/config.php
$config['dbhost'] = 'localhost';
$config['dbuser'] = 'root';
$config['dbpass'] = '';
*/

?>